<?php
require_once('../libraryfiles/config.php');

	
$action = isset($_GET['action']) ? $_GET['action'] : '';

switch($action)
{
	case 'displaypro':
		display();
		break;	
		
	case 'addpre':
		addprescription();
		break;				
	
	case 'diplayexist':		
		displayexit();
		break;
						
	case 'search_pro':
		search_pro();
		break;

	case 'delselected':		
		delselected();
		break;
		
	case 'delselectedPre':		
		delselectedPre();
		break;			

	default :
	    // if action is not defined or unknown
		// move to main user page
		header('Location: index.php');
		exit();
}

function addprescription(){
	
		// user information
		$patient_name = addslashes($_POST['patient_name']);	
		$patient_age = $_POST['patient_age'];
		$patient_sex = $_POST['patient_sex'];
		$patient_weight = $_POST['patient_weight'];
		$patient_height = $_POST['patient_height'];
		$doctor_name = $_POST['doctor_name'];
		$check_date = $_POST['check_date'];
		$pre_num = $_POST['pre_num'];
		$txtnote = addslashes($_POST['txtnote']);
		$user_id = $_SESSION['acc_id'];
		//ordernance
		$sql = "INSERT INTO tblordernance(user_id, ordernance_code, note, cdate, mdate, patient_name, sex, age, p_height, p_weight, doctor_name, check_date) 
		VALUES
		($user_id, '$pre_num', '$txtnote', NOW(), NOW(), '$patient_name', '$patient_sex', '$patient_age', '$patient_height', '$patient_weight', '$doctor_name', '$check_date')";
		mysql_query($sql);
		$ordernance_id = mysql_insert_id();
		
		//ordernance in pharmacy
		$sql_select_pha = "SELECT id FROM tbluser WHERE user_type_id = 2";
		$result_pha = mysql_query($sql_select_pha);
		while($row_pha = mysql_fetch_assoc($result_pha)){
		$phar_id = $row_pha['id'];
		$sql_pha = "INSERT INTO tblordernance_in_pharmacy(ordernance_id, pharmacy_id, pharma_check) VALUES($ordernance_id, $phar_id,0)";
		mysql_query($sql_pha);
		$oap_id[] = mysql_insert_id();
		}// end while
		
		// product in ordernance
		$qty = $_POST['qty'];
		$pro_id = $_POST['hidepro_id'];
		$count_id = $_POST['hide_count_id'];
		
		foreach($pro_id as $key => $value){
		$proid = $pro_id[$key];
		$quantity = $qty[$key];
		$made_id = $count_id[$key];
		$sql_p = "INSERT INTO tblproduct_in_ordernance(order_id, product_id, made_in, quatity) VALUES($ordernance_id, $proid, $made_id, '$quantity')";
		mysql_query($sql_p);
		$last_id = mysql_insert_id();
			foreach($oap_id as $key1 => $value1){
			// Product in ordernance for pharmacy
			$sql_ph = "INSERT INTO tblpro_order_in_pharmacy(pio_id, oip_id) VALUES($last_id,$value1)";
			mysql_query($sql_ph);	
			}
		}
			
		header("Location: success.php");
		exit;
}

function search_pro(){
		$pro_name = $_GET['searchword'];
		$same_id = $_GET['samepro'];
		if($same_id!=""){
			$sql2 = "ph.pid NOT IN ($same_id) AND ";	
		}else{
			$sql2 = "";			
		}
		$sql = "SELECT distinct ph.product_id, ph.country_id, p.*, ph.* FROM tblproduct AS p, tblproduct_in_pharmacy AS ph WHERE p.product_name LIKE '$pro_name%' AND $sql2 p.status = 1 AND ph.status = 1 AND ph.product_id = p.id GROUP BY ph.country_id, ph.product_id";
		$result = mysql_query($sql);
		if(dbNumRows($result)){
		echo '<span id="id" style="color:#999;"><em>Search Result...</em></span><br /><br />';
		$i = 1;
			while($row = mysql_fetch_assoc($result)){
				$sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = ".$row['id']." LIMIT 0,1";
				$result_img = mysql_query($sql_img);
				$row_img = mysql_fetch_assoc($result_img);
				
				if($i%2){
				$class = 'search_row1';	
				}else{
				$class = 'search_row2';	
				}
				$i++;
		?>
        <div style="width:320px; height:auto;">
        <div style="float:left; width:70px;">
        <?php
		if($row_img['thumbnail']!=""){
			$thumbnail_i = 	$row_img['thumbnail'];
		}else{
			$thumbnail_i = 	'no-image-small.png';
		}
		?>
        <img src="<?php echo W_S_IMAGES_DIR.$thumbnail_i; ?>" class="border" width="70" />
        </div>
        <div style="float:left; padding-left:15px; padding-top:10px; width:180px;"><strong style="color:#06C;"><?php echo $row['product_name']; ?></strong><br />
        <span style="color:#666;">Product type : <?php echo $row['product_type'];?></span><br />
        <span style="color:#666;">Made in : <?php echo getCountryName($row['country_id']);?></span>
        </div>
        <div style="float:right; padding-top:15px;"><a href="#1" id="<?php echo $row['id'];?>" class="addproduct"><img src="<?php echo W_ROOT;?>/images/btnadd.jpg" border="0" />
        <input type="hidden" name="count_name" value="<?php echo $row['country_id'];?>" />
        <input type="hidden" name="hidephapid" value="<?php echo $row['pid'];?>" />
        </a>
        
        </div>
        </div>
        <div style="clear:both; height:5px;"></div>
        <?php
			}// end while
		}else{
			?>
        <span id="id" style="color:#999;"><em>No Result Found !....</em></span>
        <br /><br /><br />  
            <?php
		}
}
function displayexit()
{
	$id = isset($_GET['id'])?$_GET['id']:'';	
	$count_id = isset($_GET['count_id'])?$_GET['count_id']:'';	
	$pid = isset($_GET['pid'])?$_GET['pid']:'';	
	$sql = "SELECT product_name, id FROM tblproduct WHERE id=$id";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	extract($row);
	
	$sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = ".$id." LIMIT 0,1";
				$result_img = mysql_query($sql_img);
				$row_img = mysql_fetch_assoc($result_img);
	if($row_img['thumbnail']!=""){
			$thumbnail_i = 	$row_img['thumbnail'];
		}else{
			$thumbnail_i = 	'no-image-small.png';
		}				
	?>
    <table cellpadding="0" cellspacing="0" border="0" width="100%" style="padding:0px 5px 0px 5px;" id="<?php echo $pid?>">
    <tbody>
        <tr>
            <td width="30" class="col4" align="center"><a href="#1" class="delitem" onclick="deleteItem($(this)); return false;" id="<?php echo $pid;?>" ><img src="<?php echo W_ROOT;?>/images/publish_x.png" border="0" /></a></td>
            <td width="100" align="center" class="col4"><img src="<?php echo W_ROOT;?>/seller/product/images/<?php echo $thumbnail_i; ?>" class="border" width="50" /></td>
            <td align="center" width="250" class="col4"><strong style="color:#06C;"><?php echo $product_name?></strong><br />
            <em style="color:#666;">Made in : <?php echo getCountryName($count_id );?></em>
            </td>
            <td align="center" width="150" class="col4_1">
            <input type="text" name="qty[]" style="width:100px;" class="qty" />
            <input type="hidden" name="hidepro_id[]" value="<?php echo $id;?>" />
            <input type="hidden" name="hide_count_id[]" value="<?php echo $count_id?>" />
            </td>
        </tr>
    </tbody>
    </table>
    <?php
	exit;
}

function delselected(){
	if(isset($_POST['id'])){
		
		//Statement to get selected id
		$id = (is_array($_POST['id']))?implode(',',$_POST['id']):$_POST['id'];
		
		$sql = "UPDATE tblordernance SET isDeleted = 1 WHERE ordernance_id IN ($id)";
		mysql_query($sql);
	}
	
	header('Location: viewprescription.php?'.getAllGetParams(array('view','action')));  //.'&page='.$page
	exit();	
}

function delselectedPre(){
	if(isset($_POST['id'])){
		
		//Statement to get selected id
		$id = (is_array($_POST['id']))?implode(',',$_POST['id']):$_POST['id'];
		
		$sql = "UPDATE tblordernance_in_pharmacy SET isUserDeleted = 1 WHERE id IN ($id)";
		mysql_query($sql);
	}
	
	header('Location: receiveprescription.php?'.getAllGetParams(array('view','action')));  //.'&page='.$page
	exit();	
}
?>
